// pages/activedetial/activedetial.js
var WxParse = require("../../wxParse/wxParse.js");
var api = require("../../api.js"),
  app = getApp();
var r;
const { $Toast } = require('../../iview-weapp/base/index');
Page({
  /** 页面的初始数据 */
  data: {
    /** 专家的 open_id */
    tea_open_id: '',
    index: 0,
    /** 专家详情数据 */
    expert_detial: {},
  },

  /**  生命周期函数--监听页面加载  */
  onLoad: function(options) {
    r = this;
    /** 调用是否已授权的方法 */
    getApp().is_empower();
    /** 获取传输过来的专家的 open_id */
    r.setData({
      tea_open_id: options.tea_open_id,
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    wx.showLoading({
      title: "数据加载中"
    });

    const first = new Promise((resolve, reject) => {
      /** 请求专家详情的数据 */
      wx.request({
        url: api.audio.tea_detail,
        data: {
          tea_open_id: that.data.tea_open_id
        },
        success: function(res) {
          if (res.data.code == 200) {
            that.setData({
              expert_detial: res.data.data,
            });
            resolve(res.data.data);
          }
        }
      });
    })
    first.then((res) => {
      Promise.all([this.expert_work_shops(), this.get_class_data()]).then(data => {
        setTimeout(() => {
          wx.hideLoading();
        }, 500)
      });
    }, function(err) {
      console.log(err)
    })
  },
  /** 工作坊列表 */
  expert_work_shops() {
    /** 请求专家创建的工作坊列表 */
    wx.request({
      url: api.audio.mineWorkShops,
      data: {
        openId: r.data.tea_open_id,
      },
      success: function(data) {
        r.setData({
          work_shops: data.data.data,
        });
      }
    });
  },
  /** 公司信息 */
  // getEnrolls() {
  //   wx.request({
  //     url: api.art.partnerByOpenId,
  //     data: {  openId: r.data.tea_open_id },
  //     success: function(data1) {
  //       r.setData({
  //         userInfo: data1.data.data,
  //       });
  //       console.log(that.data)
  //     }
  //   });
  // },
  /** 推荐的课程信息 */
  get_class_data() {
    wx.request({
      url: api.audio.recommendByOpenid,
      data: {
        openId: r.data.tea_open_id
      },
      success: function(res) {
        r.setData({
          class_data: res.data.data,
        });
      }
    });
  },


  /** 分享 转发 */
  onShareAppMessage: function(res) {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    wx.showNavigationBarLoading(),
      console.log("下拉刷新"),
      this.onShow(),
      setTimeout(function() {
        wx.hideNavigationBarLoading(), wx.stopPullDownRefresh();
      }, 1500);
  },

  onReachBottom: function() {},

  //跳转我的工作坊详情
  goDetails(e) {
    console.log(e)
    var o = e.currentTarget.dataset;
    wx.navigateTo({
      url: "../discoverdetail/discoverdetail?id=" + o.orderid
    });
  },
  middle: function(e) {
    const data = e.currentTarget.dataset.list;
    if (data.recType == 0) {
      wx.navigateTo({
        url: "../activedetial/activedetial?id=" + data.recOtherid

      });
    } else if (data.recType == 1) {
      wx.navigateTo({
        url: "../art/art?id=" + data.recOtherid
      });
    } else {
      wx.navigateTo({
        url: "../classdetail/classdetail?id=" + data.recOtherid
      });
    }
  },
  addCollection: function(e) {
    // console.log(e)
    var that = this;
    const opeType = e.currentTarget.dataset.collection;
    const json = that.data.detial_activity;
    const n = {
      openId: getApp().globalData.wxUserInfo.openId,
      otherId: json.copId,
      type: 4,
      opeType: opeType
    };
    if (opeType == 0) {
      n.img = that.data.detial_activity.inImg;
      n.title = that.data.detial_activity.copName;
    }
    wx.request({
      url: api.detail.add_del_Collection,
      data: n,
      method: "POST",
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success: function(res) {
        if (res.statusCode == 200) {
          if (res.data.code == 200) {
            const tips = opeType == 0 ? '已收藏' : '已取消';
            r.setData({
              isCollection: opeType == 0 ? 1 : 0
            })
            r.toast.showToast(tips);

          }

        }
      }
    });
  },

  /** 滚动事件 */
  onPageScroll(e) {
    //滚动距离+屏幕高度换算vw倍数
    let listIndex = (e.scrollTop + this.data.screenHeight) / (this.data.screenWidth * 0.63)
    this.setData({
      listIndex: listIndex
    })
  },

  /** 专家服务功能 */
  tea_service: function (event){
    let index = event.detail.index;
    /** 如果没有公司信息则跳转到添加公司的页面 */
    wx.navigateTo({
      url: "../add_enterprise/add_enterprise"
    });


    if(index == 0){
      /** 跳转到讲学的页面 */
      // wx.navigateTo({
      //   url: "../give_lectures/give_lectures"
      // });
    }
  },
  unable_ser: function(){
    $Toast({
      content: '服务暂不可用',
      type: 'warning'
    });
  }


});